package com.hispot.download;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import okhttp3.OkHttpClient;

/**
 * Created by ASUS-PC on 2016/11/13.
 */

class FileDownloadManager {

    public FileDownloadManager(OkHttpClient httpClient, String downloadDir, int threadCount) {
        this.DOWNLOAD_DIR = downloadDir;
        this.httpClient = httpClient;
        executor = Executors.newFixedThreadPool(threadCount);
    }

    public synchronized void start(String downloadUrl, String fileName) {
        if (fileDownloadImpMap.get(downloadUrl) == null){

            FileDownloadImp fileDownloadImp = new FileDownloadImp(sqLiteHelper, downloadUrl, fileName, downloadListener, this);
            fileDownloadImpMap.put(downloadUrl, fileDownloadImp);
            executor.execute(fileDownloadImp);
        }
    }

    public synchronized void pause(String url) {
        FileDownloadImp fileDownloadImp = fileDownloadImpMap.get(url);
        if (fileDownloadImp != null) fileDownloadImp.pause();
    }

    public synchronized FileDownloadModel getFileDownloadStatus(String downloadUrl) {

        FileDownloadImp fileDownloadImp = fileDownloadImpMap.get(downloadUrl);

        if (fileDownloadImp != null && fileDownloadImp.isDownloading()) {
            FileDownloadModel model = new FileDownloadModel();
            model.status = FileDownloadStatus.Downloading;
            model.percent = fileDownloadImp.getPercent();
            return model;
        }

        return FileDownloadUtils.getFileDownloadStatus(sqLiteHelper, downloadUrl);
    }

    public synchronized void delete(String downloadUrl) {
        SQLiteDatabase writableDatabase = sqLiteHelper.getWritableDatabase();
        writableDatabase.delete(FileDownloadConstant.TABLE_NAME, FileDownloadConstant.DOWNLOAD_URL + " like ?", new String[]{downloadUrl});

    }

    public void deleteAll() {
        SQLiteDatabase writableDatabase = sqLiteHelper.getWritableDatabase();
        writableDatabase.delete(FileDownloadConstant.TABLE_NAME, null, null);
    }

    public int delete(int status) {
        SQLiteDatabase writableDatabase = sqLiteHelper.getWritableDatabase();
        return writableDatabase.delete(FileDownloadConstant.TABLE_NAME, FileDownloadConstant.DOWNLOAD_STATUS + " like ?", new String[]{String.valueOf(status)});
    }

    public void setDownloadListener(FileDownloadListener downloadListener) {
        this.downloadListener = downloadListener;
    }

    public Map<String, FileDownloadImp> getFileDownloadImpMap() {
        return fileDownloadImpMap;
    }

    public String getDownloadDir(){
        return DOWNLOAD_DIR;
    }

    public void close(){
        sqLiteHelper.close();
    }

    public static OkHttpClient getHttpClient() {
        return httpClient;
    }

    private static final String TAG = "FileDownloadManager";

    private Executor executor;

    private static OkHttpClient httpClient;

    public static String DOWNLOAD_DIR = FileDownloadConstant.DEFAULT_DOWNLOAD_DIR;

    private FileDownloadListener downloadListener;

    private FileDownloadSQLiteHelper sqLiteHelper = new FileDownloadSQLiteHelper();

    private Map<String, FileDownloadImp> fileDownloadImpMap = new HashMap<>();

}
